package com.itheima.controller;

import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Value("${imgDir.name}")
    public String url;

    @Autowired
    private SetmealService setmealService;
    //上传图片
    @PostMapping("/upload.do")
    public Result upload(MultipartFile imgFile) throws IOException {
        //重新命名
        String newName = UUID.randomUUID().toString();
        //获取图片后缀名
        String oldName = imgFile.getOriginalFilename();
        String hzName = oldName.substring(oldName.lastIndexOf("."));
        //新名字拼接后缀名
        newName = newName+hzName;
        imgFile.transferTo(new File(url+newName));
        return new Result(true,"图片上传成功",newName);
    }
    //检查套餐分页展示
    @PostMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }

    //添加套餐
    @PostMapping("/add.do")
    public Result add(Integer[] checkgroupIds,@RequestBody Setmeal setmeal){
        Result result = setmealService.add(checkgroupIds,setmeal);
        return result;
    }
    //删除套餐
    @DeleteMapping("/{id}")
    public Result removeById(@PathVariable Integer id){
         return setmealService.removeById(id);
    }
    //套餐数据回显
    @GetMapping("/findById.do")
    public Result findById(Integer id){
        Result result = setmealService.findById(id);
        return result;
    }
    //修改套餐
    @PutMapping("/edit.do")
    public Result modify(Integer[] ids,@RequestBody Setmeal setmeal){
        Result result = setmealService.modify(ids,setmeal);
        return result;
    }

}
